import type { CustomReport, CustomReportField, CustomReportVO } from "@/pages/tool/customReport/entity";
import apiClient from "../../apiClient";

export interface PageListReq {
	//报表名称
	reportName?: string;
	//报表编码
	reportCode?: string;
	//参数
	params?: any;
	//当前页码
	page: number;
	//每页数量
	pageSize: number;
}

export interface PageListRes {
	//总记录数
	total: number;
	//数据列表
	records: CustomReport[];
}

const pageList = (data: PageListReq): Promise<PageListRes> =>
	apiClient.post({
		url: "/tool/report/pageList",
		data,
	});

const getInfo = (id: React.Key): Promise<CustomReportVO> =>
	apiClient.get({
		url: `/tool/report/${id}`,
	});

const add = (data: CustomReportVO): Promise<CustomReportVO> =>
	apiClient.post({
		url: "/tool/report",
		data,
	});

const remove = (ids: React.Key[]): Promise<void> =>
	apiClient.delete({
		url: "/tool/report",
		params: {
			ids: ids.join(","),
		},
	});

const parseSql = (data: CustomReportVO): Promise<CustomReportField[]> =>
	apiClient.post({
		url: "/tool/report/parse/sql",
		data,
	});

//根据表报编码获取报表字段
const getFieldsByCode = (code: string): Promise<CustomReportField[]> =>
	apiClient.get({
		url: `/tool/report/fields/${code}`,
	});

//获取报表数据
const getReportData = (data: PageListReq): Promise<PageListRes> =>
	apiClient.post({
		url: "/tool/report/data",
		data,
	});

export const customReportService = {
	pageList,
	getInfo,
	add,
	remove,
	parseSql,
	getReportData,
	getFieldsByCode,
};

export default customReportService;
